package com.alcj.controller;

import com.alcj.model.Address;
import com.alcj.model.Order;
import com.alcj.model.Station;
import com.alcj.model.User;
import com.alcj.service.AddressService;
import com.alcj.service.OrderService;
import com.alcj.service.StationService;
import com.alcj.service.UserService;
import com.alibaba.fastjson.JSONObject;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author :Liuchen
 * @version :v1.0
 * @Package :com.alcj.controller
 */
@Controller
@RequestMapping("order")
public class OrderController {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	OrderService orderService;

	@Autowired
	UserService userService;

	@Autowired
	StationService stationService;

	@Autowired
	AddressService addressService;

	@RequestMapping("/order")
	public String getOrder(String orderId, int state, Model model, HttpServletRequest request) throws Exception {
		orderId = "12";
		Order order = orderService.selectByPrimaryKey(orderId);
		System.out.println(order);

		User user = new User();
		user.setUserid(order.getUserid());


		Station station = stationService.selectStationDetailByStationId(order.getStationid());
		Address address = addressService.selectByOperaId(order.getUserid());

		user = userService.getUserInfo(user);

		model.addAttribute("order", order);
		model.addAttribute("user", user);
		model.addAttribute("address", address);
		model.addAttribute("station", station);

		model.addAttribute("disabled", state == 1 ? true : false);

		return "order";
	}

	/**
	 * @return String
	 * @Author: guofsh
	 * @Description:修改日期
	 */
	@RequestMapping("updateOrder")
	public String updateOrder(Order order) throws Exception {

		int count = orderService.updateByPrimaryKeySelective(order);
		if (count > 0) {
			System.out.println("修改成功");
		} else {
			System.out.println("修改失败");
		}

		//返回值填写 "redirect:/订单的之前的地址"
		return "redirect:/order/order?state=1";
	}

}
